package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.mapper.EmployeeMapper;
import com.sky.utils.BeanCopyUtils;
import com.sky.vo.PageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
* @author MrWang
* @description 针对表【employee(员工信息)】的数据库操作Service实现
* @createDate 2023-10-18 14:13:02
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<Employee>()
                .eq(Employee::getUsername,username);
        Employee employee = employeeMapper.selectOne(queryWrapper);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }


        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    @Override
    public Result saveEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO,employee);

        //设置账号的状态 1表示正常状态 0表示锁定状态
        employee.setStatus(StatusConstant.ENABLE);

        //设置密码 默认123456
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        //设置当前记录的创建时间和修改时间
//        employee.setCreateTime(new Date());
//        employee.setUpdateTime(new Date());

        //设置当前记录的创建人id和修改人id
        // TODO 后期改为当前登录用户的id
//        employee.setCreateUser(BaseContext.getCurrentId());
//        employee.setUpdateUser(BaseContext.getCurrentId());

        employeeMapper.insert(employee);
        return Result.success();
    }

    @Override
    public Result employeeList(EmployeePageQueryDTO employeePageQueryDTO) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>()
                .eq(Employee::getStatus,StatusConstant.ENABLE)
                .like(StringUtils.hasText(employeePageQueryDTO.getName()),Employee::getName,employeePageQueryDTO.getName());
        Page<Employee> page1 = new Page<>(Long.valueOf(employeePageQueryDTO.getPage()),Long.valueOf(employeePageQueryDTO.getPageSize()));
        employeeMapper.selectPage(page1,queryWrapper);
        List<Employee> employeeList = page1.getRecords();
        return Result.success(new PageVo(page1.getTotal(),employeeList));
    }

    @Override
    public Result updateStatus(String status, String id) {
        Employee employee = new Employee();
        employee.setId(Long.valueOf(id));
        employee.setStatus(Integer.valueOf(status));
        employeeMapper.updateById(employee);
        return Result.success();
    }

    @Override
    public Result getEmployeeById(String id) {
        Employee employee = employeeMapper.selectById(id);
        return Result.success(employee);
    }

    @Override
    public Result updateEmployee(EmployeeDTO employeeDTO) {
        Employee employee = BeanCopyUtils.copyBean(employeeDTO, Employee.class);
        employeeMapper.updateById(employee);
        return Result.success();
    }
}




